<template>
  <XtxBread>
    <XtxBreadItem to="/">首页</XtxBreadItem>
    <XtxBreadItem
      :key="category.top.id"
      v-if="category.sub"
      :to="`/category/${category.top.id}`"
      >{{ category.top.name }}</XtxBreadItem
    >
    <transition name="fade-right" mode="out-in">
      <XtxBreadItem :key="category.sub.id" v-if="category.sub">
        {{ category.sub.name }}</XtxBreadItem
      >
    </transition>
  </XtxBread>
</template>

<script>
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
export default {
  name: 'SubBread',
  setup () {
    // 利用计算属性，从vuex中获取顶级事件和二级类目信息
    const route = useRoute() // 拿id
    const store = useStore() // 拿数据

    const category = computed(() => {
      const cate = {}
      store.state.category.List.forEach((top) => {
        if (top.children) {
          const sub = top.children.find((sub) => sub.id === route.params.id)
          if (sub) {
            cate.top = { id: top.id, name: top.name }
            cate.sub = { id: sub.id, name: sub.name }
          }
        }
      })
      return cate
    })

    return { category }
  }
}
</script>

<style></style>
